const path = require('path');
const table = require('./core/table');
const api = require('./core/api');
const file = require('./helpers/file');

async function main() {
    const cwd = process.cwd();
    const sqlFile = path.join(cwd, 'sql.sql');
    let sql = file.readFile(sqlFile);
    // 去除换行符
    sql = sql.replace(/[\r\n]/g, ' ');
    const tableList = table.parse(sql);
    const apiJSON = api.parse({
        tableList,
        title: 'fast-cms',
        description: '一个简单快速的基于Vue的cms项目',
        contactName: '末烛',
        contactEmail: 'endcandle@qq.com',
        license: {
            name: 'Apache 2.0',
            url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
        },
        servers: [
            {
                url: 'https://virtserver.swaggerhub.com/end_candle/fast_cms/1.0.0',
                description: 'SwaggerHub API Auto Mocking'
            }
        ],
        security: 'BearerAuth'
    });
    file.writeFile('swagger.json', JSON.stringify(apiJSON));
}

main();
